//
//  HMHomeTableViewController.swift
//  WeiBo
//
//  Created by 李立达 on 15/12/5.
//  Copyright © 2015年 heima11. All rights reserved.
//

import UIKit
import SVProgressHUD

class HMHomeTableViewController: HMBaseVistorTableViewController {

    var statusListModel: HMStatusListViewModel
    = HMStatusListViewModel()
    
    lazy var pullupView: UIActivityIndicatorView = {
        let view = UIActivityIndicatorView(activityIndicatorStyle:UIActivityIndicatorViewStyle.WhiteLarge)
    
        view.color = UIColor.redColor()
        view.sizeToFit()
        return view
    }()
    
    lazy var refresshcontrol:HMRefresshControl = HMRefresshControl()
    
    private lazy var pullDownLable: UILabel = {
    
        let lable = UILabel(color: UIColor.whiteColor(), font: 15)
        
            lable.backgroundColor = UIColor.orangeColor()

            lable.textAlignment =  NSTextAlignment.Center
            lable.hidden = true
        
            var frame = lable.frame
            frame.size = CGSize(width: SCREENW, height: 35)
            frame.origin.y = 64 - 35
            lable.frame = frame
       
        return lable
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        
       if(!islogin)
       {
            visitorView?.isdisplayView(nil, messagelable: nil)
            return
        }
        
        self.tableView.registerClass(HMUserTableViewCell.self, forCellReuseIdentifier: HMHomeCell)
        setupUI()
        self.loadData()
       
    }
    
   private func setupUI() {
    navigationItem.leftBarButtonItem = UIBarButtonItem(imgName: "navigationbar_friendsearch", target: self, anction: "friendsearch")
    
    navigationItem.rightBarButtonItem = UIBarButtonItem(imgName: "navigationbar_pop", target: self, anction: "friendsearch")
    
    
    self.tableView.rowHeight = UITableViewAutomaticDimension
    self.tableView.estimatedRowHeight = 200
    self.tableView.separatorStyle = .None
    self.tableView.tableFooterView = pullupView
    
    
    refresshcontrol.addTarget(self, action: "loadData", forControlEvents: UIControlEvents.ValueChanged)
    
    self.tableView.addSubview(refresshcontrol)
    }
    
    
   
    
    
    
    @objc private func friendsearch(){
        let vc = HMTempController()
        navigationController?.pushViewController(vc, animated: true)
    }
}


extension HMHomeTableViewController
{

    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return statusListModel.statuslist?.count ?? 0;
    }
    
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCellWithIdentifier(HMHomeCell, forIndexPath: indexPath) as! HMUserTableViewCell
      
        cell.statusViewModel = statusListModel.statuslist![indexPath.row]
        return cell;
    }
    //MARK : 判断是不是最后一条
    override func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        
        //判读是不是最后一条,和当前有没有执行上拉加载的数据
            if !pullupView.isAnimating() && indexPath.row == statusListModel.statuslist!.count - 1
           {
            
               print("最后一条了")
               pullupView.startAnimating()
               loadData()
            }
        
    }
    
}

//MARK : 请求首页数据
extension HMHomeTableViewController
{
    func loadData(){
    
        statusListModel.loadDataFortoken(pullupView.isAnimating()) { (isSucess,count) -> () in
            
            if(!isSucess)
            {
                SVProgressHUD.showErrorWithStatus("请求失败")
                
            }
            else
            {
              print("请求成功")
              self.tableView.reloadData()
                
               if !self.pullupView.isAnimating()
               {
                    self.showmessageCount(count)
               }
            }
            self.endLoad()
        }
        
}
    //关闭动画
    private func endLoad(){
        pullupView.stopAnimating()
        refresshcontrol.endload()
    }
    func showmessageCount(count:Int){
        
        if pullDownLable.hidden == false
        {
            return
        }
        
        if self.pullDownLable.superview == nil
        {
            if let navCtrl = self.navigationController{
                navCtrl.view.insertSubview(self.pullDownLable, belowSubview: navCtrl.navigationBar)
            }
        }
        if count == 0
        {
            self.pullDownLable.text = "没有微博"
        }
        else
        {
            self.pullDownLable.text = "加载了\(count)条微博"
        }
        pullDownLable.hidden = false
        
        UIView.animateWithDuration(0.5, animations: { () -> Void in
            
             self.pullDownLable.transform = CGAffineTransformMakeTranslation(0, 35)
            }) { (_) -> Void in
             
                UIView.animateWithDuration(0.5, animations: { () -> Void in
                    
                   self.pullDownLable.transform = CGAffineTransformIdentity
                }, completion: { (_) -> Void in
                    
                    self.pullDownLable.hidden = true
                })
                
        }
            
           
    }
}